<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class BasicInfoAnnualAssessment extends Model
{
    protected $table = 'basic_info_annual_assessment'; 
    protected $guarded = [];
    protected $hidden = [
        'created_at',
        'updated_at',
        'creator_id',
        'updater_id',
    ];
    protected $appends = [
        'assess_year',
        'situation_set'
    ];

    public function getAssessYearAttribute(){
        $y = !empty($this->attributes['year']) ? $this->attributes['year'] : '';

        if(!$y){
            return now()->year;
        }

        return Carbon::parse($y)->year;
    }

    public function getSituationSetAttribute(){
        return $this->getFieldValue('situation');
    }

    public function getFieldValue($field)
    {
        $f = !empty($this->attributes[$field]) ? $this->attributes[$field] : '';

        if(!$f){
            return [];
        }

        return json_decode($f, true);  
    }
}
